//
//  KnowledgeViewController.m
//  DeepBreathing
//
//  Created by rimi on 15/12/14.
//  Copyright © 2015年 rimi. All rights reserved.
//

#import "KnowledgeViewController.h"
#import "RightViewController.h"
#import "RightViewModel.h"
#import "KnowledgeModel.h"

#import "NetworkManger+User.h"

#import "UserModel.h"

#import "KnowledgeViewModei.h"

#import "HeaderView.h"

#import "ControllerManager.h"

#import "KnowledgeTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "CellDetailsViewController.h"

@interface KnowledgeViewController ()<UITableViewDelegate,UITableViewDataSource>

//@property(nonatomic,strong)KnowledgeModel *model;

@property(nonatomic,strong)UITableView *tableview;

@property(nonatomic,strong)KnowledgeViewModei *viewModel;

@property(nonatomic,strong)RightViewModel *rightModel;

@property(nonatomic,strong)UIRefreshControl *refreshdataControl;

-(void)registerObserver;
-(void)unregiseterOberver;

@end

@implementation KnowledgeViewController
-(void)dealloc{
    
    [self unregiseterOberver];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self registerObserver];

    HeaderView *HDV  = [[HeaderView alloc]initWithTitleString:@"哮喘知识" leftImage:@"个人中心按钮_橙色" rightImage:@"收藏列表按钮" color:[UIColor whiteColor]];
    [self.view addSubview:self.tableview];
    [self.view addSubview:HDV];
    HDV.LeftDWblock = ^(){
        
        if ([ControllerManager sharedManager].drawerVC.leftOpen == NO) {
            [[ControllerManager sharedManager].drawerVC openLeftDrawer];
        }else {
            [[ControllerManager sharedManager].drawerVC closeLeftDrawer];
        }
        
        
        
    };
    HDV.RightDWblock = ^(){ 
        
        if ([ControllerManager sharedManager].drawerVC.rightOpen == NO) {
            [[ControllerManager sharedManager].drawerVC openRightDrawer];
            RightViewController * rightVC = (RightViewController *)[ControllerManager sharedManager].drawerVC.rightViewController;
            [rightVC getData123];
        }else{
            
            [[ControllerManager sharedManager].drawerVC closeRightDrawer];
        }
        
        
        
    };
    
   

    
    [NetworkManger loginWithUsername:[UserModel defaultUser].phoneNumber password:[UserModel defaultUser].password successHander:^(id responseObject) {
        [UserModel defaultUser].userID = responseObject[@"data"][@"au"][@"id"];
        [self.viewModel getData];
    } failureHander:^(NSError *error) {

    }];
//    _refreshdataControl = [[UIRefreshControl alloc]init];
//    _refreshdataControl.attributedTitle =
    

}

#pragma mark
#pragma makr -- getter
-(RightViewModel *)rightModel{
    
    if (!_rightModel) {
        _rightModel = [[RightViewModel alloc]init];
    }
    return _rightModel;
}

-(UITableView *)tableview{
    
    if (!_tableview) {
        _tableview = ({
            UITableView * tableview = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
            tableview.dataSource = self;
            tableview.delegate =self;
            [tableview registerClass:[KnowledgeTableViewCell class] forCellReuseIdentifier:@"cell"];
            tableview.rowHeight = 120;
            tableview.tableFooterView = [[UIView alloc]init];
            tableview.contentInset = UIEdgeInsetsMake(50, 0, 0, 0);
            tableview;
        
        });
    }
    
    
    
    
    return _tableview;
    
}

#pragma mark -- UITableViewDataSource
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CellDetailsViewController *CDVC = [[CellDetailsViewController alloc]init];
    
    CDVC.IDstring = [self.viewModel IdOftheKnowledge:indexPath];
    
 
        
    [self.navigationController pushViewController:CDVC animated:YES];
    
    [tableView setAccessibilityElementsHidden:NO];
    
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return [self.viewModel numberOfRows];
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    KnowledgeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    cell.titlelabel.text = [self.viewModel cellTitleAtIndexPath:indexPath];
    [cell.logoiamgeView sd_setImageWithURL:[self.viewModel cellImageUrlAtIndexPath:indexPath]];
    cell.dateLabel.text = [self.viewModel cellDateAtIndexPath:indexPath];
    
    
    
    return cell;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return CGFLOAT_MIN;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    
    return 10;
}


-(KnowledgeViewModei *)viewModel{
    
    if (!_viewModel) {
        _viewModel = [[KnowledgeViewModei alloc]init];
    }
    
    return _viewModel;
}








//-(KnowledgeModel *)model{
//    
//    if (!_model) {
//        _model = [[KnowledgeModel alloc]init];
//    }
//    return _model;
//}

#pragma mark -- private methods
-(void)registerObserver{
    
    [self.viewModel     addObserver:self forKeyPath:[self.viewModel observeringKeyPath] options:NSKeyValueObservingOptionNew context:nil];
    
    
}
-(void)unregiseterOberver{
    
    [self.viewModel     removeObserver:self forKeyPath:[self.viewModel observeringKeyPath]];
    
}

#pragma  mark -- ovvride
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{
    if ([self.viewModel isSuccess]) {
        //更新界面
        [self.tableview reloadData];
    }else{
        //请求失败，弹出提示框
    }
    
    
    
    
    
    
}
@end
